#include "mainwindowimpl.h"
#include "loginUi.h"
#include "actions.h"
#include "dialogcadpocoimpl.h"


extern loginUi *login;

MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) 
	: QMainWindow(parent, f), SocketClient()
{
	setupUi(this);
	connect(sck, SIGNAL(readyRead()), this, SLOT(dataReady()));
    connect(sck,SIGNAL(connected()),this,SLOT(hostFound()));
    connect(actionCadastrar_Poco,SIGNAL(activated()),SLOT(CadastratPocos()));
    connect(actionDelete_Well,SIGNAL(activated()),this,SLOT(deleteSlot()));
    connect(actionHistoric,SIGNAL(activated()),this,SLOT(historicSlot()));
	connect(pushButton,SIGNAL(clicked()),this,SLOT(con()));
	connect(wellTable,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(tableClicked(int,int)));
	connect(wellTable,SIGNAL(cellChanged(int,int)),this,SLOT(tableChanged(int,int)));	
	connect(sck,SIGNAL(disconnected()),this,SLOT(onDisconnect()));
}

void MainWindowImpl::onDisconnect() {
	this->hide();
	login->show();
	login->lb_status->setText("Connection Reset by peer");
}
void MainWindowImpl::tableChanged(int row,int col) {
	QStringList ToSend;
	if(col != 5)
		return;
	int i = wellTable->item(row,5)->text().toInt();
	
	if(i == wMap[wellTable->item(row,0)->text()].timeout)
		return;
	if(i == 0) {
		wellTable->item(row,5)->setText(QString::number(wMap[wellTable->item(row,0)->text()].timeout));
		return;
	}
	ToSend.push_back(wellTable->item(row,0)->text());
	ToSend.push_back(wellTable->item(row,5)->text());
	wMap[wellTable->item(row,0)->text()].timeout = wellTable->item(row,5)->text().toInt(); 
	sendFunction(CHANGE_TIMEOUT_WELL,ToSend);
	printf("MUDOU ALGUMA COISA\n");
}

void MainWindowImpl::historicSlot(){
	login->hist.show();	
}

void MainWindowImpl::deleteSlot(){
	login->del.show();	
}

void MainWindowImpl::tableClicked(int row,int col) {
	QStringList ToSend;
	if(col == 5)
		return;
	printf("CLICKADO %s %i\n",wellTable->item(row,0)->text().toStdString().c_str(),wMap[wellTable->item(row,0)->text()].idSimulador);
	ToSend.push_back(wellTable->item(row,0)->text());
	if(wMap[wellTable->item(row,0)->text()].state < 0 || wMap[wellTable->item(row,0)->text()].state > 3)
		return;
	ToSend.push_back(QString::number(wMap[wellTable->item(row,0)->text()].idSimulador));
	sendFunction(GET_ALL_DATA_FROM_WELL,ToSend);
	sendFunction(GET_LETTER_FROM_WELL,ToSend);
	//login->win.getHistoricFromWell(wellTable->item(row,0)->text());
}

void MainWindowImpl::CadastratPocos() {
	login->ui.show();
	QStringList Param;
}

void MainWindowImpl::con() {
	requestWellList();
}

void MainWindowImpl::dataReady() {
	receiveData();
}

void MainWindowImpl::hostFound() {
	printf("connectado com sucesso %s %s\n",login->le_pass->text().toStdString().c_str(),
								login->ln_login->text().toStdString().c_str());
	loginRequest(login->le_pass->text(),login->ln_login->text());
}

void printFrame(QStringList frame) {
		while(frame.size() != 0) {
		printf("PRINTFUAME %s\n",frame.front().toStdString().c_str());
		frame.pop_front();
	}
	printf("\n\n");
}

void MainWindowImpl::receiveData() {
	printf("Dados\n");
	QStringList frame;
	QString Par;
	int intPar;
		
	QDataStream in(this->sck);
	in.setVersion(QDataStream::Qt_4_0);

	while(this->sck->bytesAvailable()) {
		in >> frame;
		//printFrame(frame);
		Par = frame.front();
		intPar = Par.toInt();
		frame.pop_front();
		switch(intPar) {
			case HAND_SHAKE:
				actions::loginAction(frame);						
			break;
			case GET_WELL_LIST:
				printf("RECEBI UM HAND 2 SHAKE\n");
				actions::weelListAction(frame);
			break;
			case REGISTER_WELL:
				printf("RECEBI REGISTER_WELL\n");
				actions::registerWellAction(frame);
			break;
			case GET_STATE_WELL:
				actions::getStateWellAction(frame);
			break;
			case GET_MASTERS_LIST:
				actions::getMasterListAction(frame);
			break;
			case GET_ALL_DATA_FROM_WELL:
				actions::getAllDataFromWellAction(frame);
			break;
			case GET_LETTER_FROM_WELL:
				actions::getLetterFromWellAction(frame);
			break;
			case DELETE_WELL:
				actions::getDeleteWellAction(frame);
			break;
			case GET_ALARMS_FROM_WELL:
				actions::getAlarmsAction(frame);
			break;
			case GET_HISTORIC_FROM_WELL:
				actions::getHistoricAction(frame);
			break;
			default:
			break;
	}
	}
	this->sck->flush();
}



